package com.jk.service.account;

import com.jk.dao.account.AccPermissionDao;
import com.jk.entity.account.AccPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.dao.DataAccessException;

import java.util.ArrayList;
import java.util.List;

/**
 * ACC_PERMISSION service
 * Tue Sep 22 11:02:19 CST 2015 yanghuan
 */
@Service
public class AccPermissionService {

    @Autowired
    private AccPermissionDao accPermissionDao;

    public int delete(String accPermissionId) throws DataAccessException {
        return accPermissionDao.deleteByPrimaryKey(accPermissionId);
    }

    public int insert(AccPermission accPermission) throws DataAccessException {
        return accPermissionDao.insert(accPermission);
    }

    public List<AccPermission> getList() throws DataAccessException {
        //获取所有权限
        List<AccPermission> permissionList = accPermissionDao.selectAll();
        if (null == permissionList) {
            permissionList = new ArrayList<AccPermission>();
        }
        return this.getPermissions(permissionList, "root");
    }

    private List<AccPermission> getPermissions(List<AccPermission> permissionList, String pid) {
        List<AccPermission> list = new ArrayList<AccPermission>();
        for (AccPermission accPermission : permissionList) {
            if (pid.equals(accPermission.getAccPermissionParentId())) {
                accPermission.setChildren(getPermissions(permissionList, accPermission.getAccPermissionId()));
                list.add(accPermission);
            }
        }
        return list;
    }
}

